
import org.cgfy.generator.Driver;
import org.cgfy.generator.constant.DM;
import org.cgfy.generator.constant.MySQL;
import org.cgfy.generator.constant.Oracle;
import org.cgfy.generator.models.ClassContent;
import org.cgfy.generator.models.Template;
import org.cgfy.generator.options.DatabaseOptions;
import org.cgfy.generator.options.Options;
import utils.PropertiesUtil;

import java.util.HashMap;
import java.util.Map;

public class Executor {
    //远程
    public static final String jdbcUrl = PropertiesUtil.getDbValue("jdbcUrl");
    public static final String jdbcUser = PropertiesUtil.getDbValue("jdbcUser");
    public static final String jdbcPassword = PropertiesUtil.getDbValue("jdbcPassword");
    public static final String tableName =  PropertiesUtil.getDbValue("tableName");
    public static final String javaFileName =  PropertiesUtil.getDbValue("javaFileName");
    public static final String databaseType = PropertiesUtil.getDbValue("databaseType");
    public static final String prePackageUrl = PropertiesUtil.getDbValue("prePackageUrl");
    public static final String packageUrl = PropertiesUtil.getDbValue("packageUrl");
    public static final String DOMAIM_URL ="entity";
    public static final String DAO_URL ="dao.shop.write";
    public static final String SERVICE_URL ="service";
    public static final String SERVICE_IMPL_URL ="service.impl";
    public static final String MODEL_URL ="model";
    public static final String XML_URL ="sql-mapper.shop.write";


    public static void main(String[] args){
        try {//com.yixiekeji.dao.shop.write.
            String javaTemplatePath="xfbf";//模版路径
            Map<String, Template> templates = new HashMap<String, Template>();
            String outputPath=System.getProperty("user.dir");
            System.setProperty("user.dir",System.getProperty("user.dir")+"/templates");
            //java生成
            templates.put("entity", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("EntityLombok.ftl")//Lombok模板为EntityLombok.ftl
                    .setPackageName(prePackageUrl+"."+DOMAIM_URL+"."+packageUrl)
                    .setClassName(javaFileName));
            templates.put("dao", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Dao.ftl")
                    .setPackageName(prePackageUrl+"."+DAO_URL+"."+packageUrl)
                    .setClassName(javaFileName+"WriteDao"));
            templates.put("mapper", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Mapper.ftl")
                    .setSourceFolder("src/main/resources")
                    .setPackageName(XML_URL+"."+packageUrl)
                    .setClassName(javaFileName+"WriteMapper")
                    .setSuffix(".xml"));
            //生成拓展
            templates.put("service", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Service.ftl")
                    .setPackageName(prePackageUrl+"."+SERVICE_URL+"."+packageUrl)
                    .setClassName("I"+javaFileName +"Service"));
            templates.put("serviceImpl", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("ServiceImpl.ftl")
                    .setPackageName(prePackageUrl+"."+SERVICE_IMPL_URL+"."+packageUrl)
                    .setClassName(javaFileName  +"ServiceImpl"));
            templates.put("model", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Model.ftl")
                    .setPackageName(prePackageUrl+"."+MODEL_URL+"."+packageUrl)
                    .setClassName(javaFileName+"Model"));



            DatabaseOptions databaseOptions=null;
            // 数据库连接信息
            if("mysql".equals(databaseType)){
                databaseOptions = new DatabaseOptions(MySQL.MYSQL_DERVER, jdbcUrl,jdbcUser,jdbcPassword);
            }else if ("mysql8".equals(databaseType)){
                databaseOptions = new DatabaseOptions(MySQL.MYSQL_8_DERVER, jdbcUrl,jdbcUser,jdbcPassword);
            }else if ("oracle".equals(databaseType)){
                databaseOptions = new DatabaseOptions(Oracle.ORACLE_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }else if ("dm".equals(databaseType)){
                databaseOptions = new DatabaseOptions(DM.DM_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }
            // 初始化执行工具
            Driver screwDriver = Driver.instance(new Options(databaseOptions).setOutputPath(outputPath+"/GENERATE/"));
            // 读取表信息
            ClassContent content = screwDriver.readTable(tableName);
            // 创建代码
            screwDriver.createCode(content,templates);
            System.out.println("生成成功!");
        }catch (Exception e){
            System.out.println("生成失败,错误信息:"+e.getMessage());
            e.printStackTrace();
        }

    }
}
